package com.lancoo.answer.view.base

import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import java.util.*

abstract class BaseRecyclerAdapter : RecyclerView.Adapter<BaseRecyclerViewHolder>() {
    protected var _data: List<*>? = null
    var listState = ListState.NONE

    enum class ListState {
        NONE, REFRESHING, LOADMOREING
    }

    var data: List<*>?
        get() = if (_data == null) ArrayList<Any?>().also { _data = it } else _data
        set(data) {
            _data = data
            notifyDataSetChanged()
        }

    override fun getItemCount(): Int {
        return if (_data == null) {
            0
        } else _data!!.size
    }

    fun getItem(position: Int): Any? {
        return if (_data != null && _data!!.size > position) {
            _data!![position]
        } else null
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseRecyclerViewHolder {
        return BaseRecyclerViewHolder(getHolderView(parent, viewType)!!)
    }

    abstract fun getHolderView(parent: ViewGroup?, position: Int): View?
}